package com.qd.panda.stroke;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.pay.PayTestService;
import com.qd.panda.service.stroke.UserStrokeHolderService;
import com.qd.pay.domain.CloseOrderDTO;
import com.qd.pay.domain.OrderRefundDTO;
import com.qd.pay.service.TfBillService;
import com.qd.pay.service.order.OrderHolderService;
import com.qd.tfpay.order.AccountBO;
import com.qd.tfpay.order.OrderApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * TEST_用户行程
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/test/userStroke/")
@Slf4j
public class TestStrokeController {
    private final OrderHolderService orderHolderService;
    private final OrderApi orderApi;
    private final UserStrokeHolderService userStrokeHolderService;
    private final TfBillService tfBillService;

    private final PayTestService payTestService;

    /**
     * 付款以后，测试使用
     *
     * @param orderId 订单ID
     * @return true or false
     */
    @GetMapping(value = "/afterPay")
    public ApiResultWrapper<Object> afterPay(@Validated @NotBlank(message = "订单ID不能为空") String orderId, Integer payStatus, Integer orderType) {
        userStrokeHolderService.afterPay(orderId, payStatus, orderType);
        return ApiResult.success("ok");
    }

    /**
     * 退款以后，测试使用
     *
     * @return true or false
     */
    @GetMapping(value = "/afterRefund")
    public ApiResultWrapper<Object> afterRefund(String sid, String refundId, Integer refundStatus) {
        userStrokeHolderService.afterRefund(sid, refundId, refundStatus);
        return ApiResult.success("ok");
    }

    /**
     * 关闭订单，测试使用
     *
     * @return true or false
     */
    @GetMapping(value = "/closeOrder")
    public ApiResultWrapper<Object> closeOrder(String orderId) {
        CloseOrderDTO closeOrder = new CloseOrderDTO();
        closeOrder.setAppId(SecurityUtils.getLoginUser().getUser().getAppUserExtend().getAppId());
        closeOrder.setThirdPartyOrderId(orderId)
                .setType(2);
        return userStrokeHolderService.closeOrder(closeOrder);
    }

    /**
     * 退款，测试使用
     *
     * @param orderId    订单ID
     * @param payAmount  订单金额
     * @param refundFee  退款金额
     * @param refundDesc 退款原因
     * @return true or false
     */
    @GetMapping(value = "/refundOrder")
    public ApiResultWrapper<Object> refundOrder(String orderId, BigDecimal payAmount, BigDecimal refundFee, String refundDesc) {
        if (null == refundFee) {
            refundFee = payAmount;
        }
        if (!StringUtils.hasLength(refundDesc)) {
            refundDesc = "测试退款";
        }
        OrderRefundDTO dto = new OrderRefundDTO()
                .setAppId(SecurityUtils.getLoginUser().getUser().getAppUserExtend().getAppId())
                .setThirdPartyOrder(orderId)
                .setPayAmount(payAmount)
                .setRefundFee(refundFee)
                .setRefundDesc(refundDesc)
                .setType(2);
        return userStrokeHolderService.refundOrder(dto);
    }

    /**
     * 测试下载对账文件
     *
     * @param date 日期
     * @param type 0
     * @return true or false
     */
    @GetMapping(value = "/downloadBill")
    public ApiResultWrapper<Object> downloadBill(@Validated String date, int type, @NotBlank(message = "主商户号不能为空") String partner, String subPartner) throws IOException {
        if (type == 0) {
            AccountBO accountBO = orderApi.downloadAccount(partner, subPartner, date, "0001");
            return ApiResult.success(accountBO);
        } else {
            tfBillService.downloadBill(date, partner);
            return ApiResult.success("下载对账单成功");
        }
    }

    /**
     * 查询订单，测试使用
     *
     * @return true or false
     */
    @GetMapping(value = "/orderState")
    public ApiResultWrapper<Object> orderState(String orderId) {
        return orderHolderService.orderState(orderId, "orderReport;orderRefund", null, 2);
    }

    /**
     * 更新订单回执状态，测试使用
     *
     * @return true or false
     */
    @GetMapping(value = "/testOrderState")
    public ApiResultWrapper<Object> testOrderState(String orderId) {
        if (StringUtils.hasLength(orderId)) {
            payTestService.testOrderState(orderId);
        } else {
            payTestService.testOrderState();
        }
        return ApiResult.success();
    }
}
